package com.LeeCode;

/**
 * 买卖股票的最佳时机
 */

public class Code121 {
    public static void main(String[] args) {
        int[] nums = {7, 1, 5, 3, 6, 4};
        System.out.println(new Code121().maxProfit(nums));
    }

    public int maxProfit(int[] prices) {
        int min = prices[0], ans = 0;

        for (int i = 1; i < prices.length; i++) {
            if (min > prices[i]) { // 考虑在在天买入
                min = prices[i];
            } else { // 考虑在在天卖出
                ans = Math.max(ans, prices[i] - min);
            }
        }
        return ans;
    }
}
